package cn.kgc.kgde.bookstore.controller;

import cn.kgc.kgde.bookstore.entity.Address;
import cn.kgc.kgde.bookstore.entity.ResultInfo;
import cn.kgc.kgde.bookstore.mapper.AddressMapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author myt
 * @since 2022-05-23
 */
@RestController
@RequestMapping("/address")
public class AddressController {
    @Resource
    private AddressMapper addressMapper;

    //添加地址
    @PostMapping("add")
    public ResultInfo add(Address address){
        System.out.println("address = " + address);
        this.addressMapper.insert(address);
        return new ResultInfo(200,"添加成功",address);
    }

    //查看地址列表
    @GetMapping("showList")
    public ResultInfo showList(Address address){
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.select("*")
                .eq("userCode",address.getUserCode());
        List<Address> addressList = this.addressMapper.selectList(wrapper);
        System.out.println("addressList = " + addressList);
        return new ResultInfo(200,"查询成功",addressList);
    }

    //删除地址
    @PostMapping("delete")
    public ResultInfo delete(Address address){
        this.addressMapper.deleteById(address);
        return new ResultInfo(200,"删除成功");
    }

    //修改地址
    @PostMapping("modify")
    public ResultInfo modify(Address address){
        this.addressMapper.updateById(address);
        return new ResultInfo(200,"修改成功",address);
    }
}
